<?php

namespace Api\Event;


/** 
 * 点赞接口
 * @author Legend. <xcx_legender@qq.com>
 * @param 
 * @return 
 */
use Api\Manage\SysNoticeManage;

class LikeEvent extends BaseEvent 
{

	/** 
	 * 是否点赞
	 * @author Legend. <xcx_legender@qq.com>
	 * @param $uid 是否赞了 $target_id
	 * @return 
	 */
	
	public function is_like_video($uid, $target_id)
	{
		$service = D('Common/Like', 'Service');
		$map['uid']       = intval($uid);
		$map['target_id'] = intval($target_id);
		$map['type']      = 0;
		return $service->where($map)->count() > 0;
	}

	// 是否对用户点赞了
	public function is_like_user($uid, $target_id)
	{
		$service = D('Common/Like', 'Service');
		$map['uid']       = intval($uid);
		$map['target_id'] = intval($target_id);
		$map['type']      = 2;
		return $service->where($map)->count() > 0;
	}

	/** 
	 * 点赞
	 * @author Legend. <xcx_legender@qq.com>
	 * @param 
	 * @return 
	 */
	
	public function add($type, $uid, $target_id, &$ret_data, $extend = [])
	{
	 	if (!$target_id)
		{
			return;
		}
		$service = D('Common/Like', 'Service');
		// uid 可以为0
		$map['uid']       = $uid = intval($uid);
		$map['target_id'] = $target_id = intval($target_id);
		$map['type']      = $type;
		$nickname = $uid ? get_nickname($uid) : getVisitorName();
		// 游客或者已经点赞了
		if ($uid == 0 || !$service->where($map)->find())
		{	
			$map['create_time'] = NOW_TIME;
			if (!$uid) $map['nickname'] = $nickname;
			$service->add($map);	
			$sender = [
					'uid'      => $uid,
					'nickname' => $nickname,
				];

			// 视频
			if ($type == 0)
			{
				$video = A('Api/Video','Event')->detail($target_id);
				event('Statis')->operStatisVideo($target_id, $uid, 'zan', 1, $extend);
				if($uid > 0){
					$score = event('Score')->get($uid, 'zan', $target_id);
					$score != false && $ret_data['score_plus'] = $score;
				}

				// 处理imba_video表的数据  liubo 2016-06-16
				service('Video')->where(['id' => $target_id])->setInc('zan', 1);

				// 游客点赞不发送通知[临时] by liubo 2016-06-27
				if ($uid == 0) {
					return true;
				}
				event('Message')->send('zan', $target_id, $uid, $video['uid']);
				// $to = get_nickname($video['uid']);
				$toUser = [
					'uid'      => $video['uid'],
					'nickname' => get_nickname($video['uid']),
				];
				// 对自己视频点赞不推送
				if ($uid != $video['uid']) {
					event('Push')->videoZan($sender, $toUser, $video);
				}
				SysNoticeManage::afterLike($video, $sender);
			}else if ($type == 2){ // 用户
				// $to = get_nickname($target_id);
				$toUser = [
					'uid'      => $target_id,
					'nickname' => get_nickname($target_id),
				];

				// 添加message数据 liubo 2016-07-13
				event('Message')->send('like_user', 0, $uid, $target_id);
				event('Push')->like($sender, $toUser);
			} else if ($type == 3) { // 评论
				$condition = ['id' => $target_id];
				service('Comments')->where($condition)->setInc('zan', 1);
			}
			return true;
		}
		return false;
	 }


	 /** 
	  * 取消
	  * @author Legend. <xcx_legender@qq.com>
	  * @param 
	  * @return 
	  */
	 //评论取消点赞 liubo 2016-06-14
	 public function cancel($type, $uid, $target_id)
	 {
	 	if (!$uid || !$target_id)
		{
			return;
		}
		$service = D('Common/Like', 'Service');
		$map['uid']       = intval($uid);
		$map['target_id'] = intval($target_id);
		$map['type']      = $type;
		if (!$service->where($map)->find()) {
			return false;	
		}
		$service->where($map)->delete();
		switch ($type) {
			case 3:
				service('Comments')->where(['id' => $target_id])->setDec('zan', 1);
				break;
			case 0:
				// 处理imba_video表的数据  liubo 2016-06-16
				service('Video')->where(['id' => $target_id])->setDec('zan', 1);
			default:
				event('Statis')->operStatisVideo($target_id, $uid, 'zan', -1);
		}
		return ture;
	 }

	 protected function statis_do($type, $id, $field, $value)
	 { 
	 	/*$cacheEvent = A('Api/Cache', 'Event');
	 	$statis = $cacheEvent->videoStatis($id, 'get', $field);
	 	$statis = intval($statis);
	 	$value = $statis + $value > 0 ? $statis + $value : 0;
	 	$cacheEvent->videoStatis($id, 'set', $field, $value);*/
	 	

	 	// $service = D('Common/Video', 'Service');
	 	// $map['id'] = $id;
	 	// $service->where($map)->setInc($field, $value);
	 	// echo $service->_sql();
	 }

	 /**
	 *获取视频点赞用户的信息
	 *@param int $vid 视频id
	 *@param int $type 0:视频点赞用户，1：赛事点赞
	 *@return array
	 */
	 public function getUser($vid, $limit=0, $p=1){

	 	$user = array();

	 	$service = D('Common/Like', 'Service');

	 	$map = array(
	 			'target_id' => intval($vid),
	 			'uid'       => ['gt', 0]
	 		);

	 	$res = $service->where($map);
	 	$limit && $res=$res->limit($limit);
	 	$p && $res = $res->page($p);
	 	//$uids = $res->where($map)->order('create_time desc')->getField('uid', true);
	 	$infos = $res->where($map)->order('create_time desc')->field('uid,nickname,create_time')->select();
		$uEvent = A('Api/User', 'Event');
		if ($infos){
			foreach ($infos as $key => $value) {
				$res = $uEvent->info($value['uid']);
				$res['nickname'] = !empty($value['nickname']) ? $value['nickname']: $res['nickname'];
				$data = [
					'uid' => $value['uid'],
					'nickname' => $res['nickname'],
					'avatar' => $res['avatar'],
					'create_time' => $value['create_time'],
				];
				$res && $data['user'] = $res;
				$user[] = $data;
			}
		}
		

	 	return $user;
	 } 

	public function userlist($map = [], $field = '', $p = 1, $limit = 10)
	{
		$default_field = 'id,nickname,avatar';
		$user = array();

		$map['uid'] = ['$gt' => 0];
		$query = service('Like')->where($map);
		if ($p && $limit) {
			$query->page($p)->limit($limit);
		}
		$uids = $query->getField('uid', true);
		if (!empty($uids)) {
			$users = service('Users')->where(['id' => ['$in' => $uids]])->field($field?:$default_field)->select();
		}
		return empty($users) ? [] : array_values($users);
	}
}